<template>
  <div class="box">
    <h2>
      商品管理页面<small
        ><el-button type="primary" size="small" round @click="showDialog()"
          >新增商品</el-button
        ></small
      >
    </h2>
    <el-table :data="goods.goodsList" height="600" style="width: 100%">
      <el-table-column type="expand">
        <template #default="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品编号">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="商品名称">
              <span>{{ props.row.goodsname }}</span>
            </el-form-item>
            <el-form-item label="商品图片">
              <span
                ><img
                  :src="'http://localhost:3000' + props.row.img"
                  style="height: 100px"
              /></span>
            </el-form-item>
            <el-form-item label="商品单价">
              <span>{{ props.row.price }}</span>
            </el-form-item>
            <el-form-item label="市场价格">
              <span>{{ props.row.market_price }}</span>
            </el-form-item>
            <el-form-item label="一级分类编号">
              <span>{{ props.row.first_cateid }}</span>
            </el-form-item>
            <el-form-item label="一级分类名称">
              <span>{{ props.row.firstcatename }}</span>
            </el-form-item>
            <el-form-item label="二级分类名称">
              <span>{{ props.row.secondcatename }}</span>
            </el-form-item>
            <el-form-item label="商品规格编号">
              <span>{{ props.row.specsid }}</span>
            </el-form-item>
            <el-form-item label="商品规格">
              <span>{{ props.row.specsattr }}</span>
            </el-form-item>
            <el-form-item label="是否新品">
              <el-tag type="success" size="small" v-if="props.row.isnew === 1"
                >新品</el-tag
              >
              <el-tag type="info" size="small" v-else>非新品</el-tag>
            </el-form-item>
            <el-form-item label="是否热卖">
              <el-tag type="success" size="small" v-if="props.row.ishot === 1"
                >热卖</el-tag
              >
              <el-tag type="info" size="small" v-else>非热卖</el-tag>
            </el-form-item>
            <el-form-item label="商品描述">
              <span v-html="props.row.description"></span>
            </el-form-item>
            <el-form-item label="商品状态">
              <el-tag type="success" size="small" v-if="props.row.status === 1"
                >启用</el-tag
              >
              <el-tag type="info" size="small" v-else>禁用</el-tag>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品 ID" prop="id"> </el-table-column>
      <el-table-column label="商品名称" prop="goodsname"> </el-table-column>
      <el-table-column label="商品图片" prop="img">
        <template v-slot="prop">
          <img
            :src="'http://localhost:3000' + prop.row.img"
            style="height: 100px"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column label="商品单价" prop="price"> </el-table-column>
      <el-table-column label="一级分类" prop="firstcatename"> </el-table-column>
      <el-table-column label="二级分类" prop="secondcatename">
      </el-table-column>
      <el-table-column prop="status" label="商品状态">
        <template v-slot="prop">
          <el-tag size="small" v-if="prop.row.status === 1">启用</el-tag>
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="180">
        <template v-slot="props">
          <el-button
            type="success"
            size="small"
            round
            @click="editGoods(props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            round
            @click="delGoods(props.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="goods.page"
      v-model:page-size="goods.size"
      :page-sizes="[1, 3, 5, 10, 20, 50, 100]"
      small
      :total="goods.total"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changeSize"
      @current-change="changePage"
    />
    <!-- 弹窗 -->
    <Dialog ref="VDialog"></Dialog>
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from "element-plus";
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
// 导入弹窗组件
import Dialog from "./dialog.vue";
// 导入store
import useGoodStore from "../../stores/goods";
// 定义
const goods = useGoodStore();
//
const $router = useRouter();

// 获取数据
onMounted(() => {
  // 调用函数,获取菜单数据
  goods.getGoodsListActions();
  goods.getGoodsTototalActions();
});

// 分页相关函数
function changeSize(size) {
  goods.CHANGE_SIZE(size);
  goods.getGoodsListActions();
}
function changePage(page) {
  // console.log(page);
  goods.CHANGE_PAGE(page);
  goods.getGoodsListActions();
}

// 弹窗变量
const VDialog = ref(null);

// 打开弹窗
const showDialog = () => {
  VDialog.value.dialogVisible = true;
  // console.log(VDialog.value.dialogVisible);
};

//删除类别
function delGoods(Goods) {
  // console.log("Goods", Goods);
  ElMessageBox.confirm("确定删除此商品吗？", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 调用删除函数
      goods.delGoodsActions(Goods.id);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除此商品",
      });
    });
}

// 编辑类别
function editGoods(Goods) {
  // console.log("11111111111111", VDialog.value,Goods.id);
  // 显示弹窗，将数据发送给子组件
  VDialog.value.acceptValue(Goods);
  showDialog();
}
</script>

<style lang="css" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 100%;
  border-bottom: 1px dashed rgb(143, 143, 143);
}
.el-pagination {
  margin-top: 20px;
  justify-content: center;
}
</style>
